﻿using System.Drawing;
using System.Windows.Forms;
using System;
using System.Drawing.Drawing2D;

namespace HurlBat.XEdit2D
{
    public partial class Arrow //: UserControl
    {
        Node start = null;
        Node end = null;

        public Arrow()
        {
            //InitializeComponent();
            //this.BackColor = Color.FromArgb(0, Color.White);
        }

        public void Draw(Graphics g, Panel pnl)
        {
            if (this.start == null) return;
            Pen p = new Pen(Color.Gray, 2);
            p.EndCap = System.Drawing.Drawing2D.LineCap.Custom;
            p.CustomEndCap = new AdjustableArrowCap(4, 7, true);
            if (end == null) p.DashStyle = DashStyle.Dash;
            Point startPoint = Point.Empty;
            Point endPoint = end == null ? pnl.PointToClient(Control.MousePosition) : end.Location;
            
            if (start.Right <= (end == null ? endPoint.X : end.Left))
            {
                startPoint.X = start.Right;
                if (end != null) endPoint.X = end.Left;
            }
            else if (start.Left <= (end == null ? endPoint.X : end.Right))
            {
                startPoint.X = end == null ? endPoint.X : end.Left + (start.Right - end.Left) / 2;
                if (end != null) endPoint.X = startPoint.X;
            }
            else
            {
                startPoint.X = start.Left;
                if (end != null) endPoint.X = end.Right;
            }

            if (start.Bottom <= (end == null ? endPoint.Y : end.Top))
            {
                startPoint.Y = start.Bottom;
                if (end != null) endPoint.Y = end.Top;
            }
            else if (start.Top <= (end == null ? endPoint.Y : end.Bottom))
            {
                startPoint.Y = end == null ? endPoint.Y : end.Top + (start.Bottom - end.Top) / 2;
                if (end != null) endPoint.Y = startPoint.Y;
            }
            else
            {
                startPoint.Y = start.Top;
                if (end != null) endPoint.Y = end.Bottom;
            }
            
            //startPoint.X = Math.Min(start.Right, end.Left);
            //startPoint.Y = Math.Min
            g.DrawLine(p, startPoint, endPoint);
            //g.DrawBezier(p, startPoint, new Point(startPoint.X + 5, startPoint.Y),
            //    new Point(startPoint.X + (endPoint.X - startPoint.X) / 2, startPoint.Y + (endPoint.Y - startPoint.Y) / 2),
            //    endPoint);
        }

        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);
        //    Pen p = new Pen(Color.Black, 2);
        //    p.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
        //    //if (isResizing)
        //    //    p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
        //    //Point[] points = new Point[] { new Point(0,0), new Point(5,2), new Point(this.Width/2, this.Height/2),
        //    //    new Point(this.Width-5, this.Height-2), new Point(this.Width, this.Height) };
        //    //e.Graphics.DrawBezier(p, new Point(0, 0), new Point(this.Width / 2, 0), 
        //    //    new Point(this.Width/2, this.Height), new Point(this.Width, this.Height));
        //    e.Graphics.DrawLine(p, 0, 0, this.Size.Width, this.Size.Height);
        //    //e.Graphics.DrawRectangle(p, new Rectangle(0, 0, this.Width - 1, this.Height - 1));
        //}

        public void SetStartNode(Node n)
        {
            start = n;
        }

        public bool SetEndNode(Node n)
        {
            if (!n.AddInput(start)) return false;
            start.AddArrow(this);
            end = n;
            return true;
        }
    }
}
